/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.manager;

import com.google.common.collect.Lists;
import com.lvyh.lightframe.job.admin.listener.ScheduleServerChangeListener;
import com.lvyh.lightframe.job.admin.util.ByteUtils;
import com.lvyh.lightframe.job.admin.util.DateUtils;
import com.lvyh.lightframe.job.enums.ServerNodeChange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Set;

public abstract class AbstractScheduleServerManager {
    private final static Logger logger = LoggerFactory.getLogger(AbstractScheduleServerManager.class);

    /**
     * online nodes
     */
    public static final String SERVER_PATH = "/servers";

    /**
     * offline nodes
     */
    public static final String OFFLINE_SERVER_PATH = "/offline-server";
    /**
     * Scheduling node temporary tasks
     */
    public static final String SERVER_TMP_JOBS = "/tmp-jobs";

    @Autowired
    protected ZkManager zkManager;

    private List<ScheduleServerChangeListener> scheduleServerChangeListeners = Lists.newCopyOnWriteArrayList();


    public void addScheduleServerChangeListener(ScheduleServerChangeListener listener) {
        this.scheduleServerChangeListeners.add(listener);
    }

    /**
     * Trigger event
     */
    protected void triggerScheduleServerChangeListener(ServerNodeChange serverNodeChange) {
        if (this.scheduleServerChangeListeners == null) {
            return;
        }
        if (serverNodeChange == ServerNodeChange.DATA_CHANGED) {
            Set<String> currentServerNodes = getCurrentServerNodes();
            for (ScheduleServerChangeListener listener : this.scheduleServerChangeListeners) {
                listener.serverNodeChanged(currentServerNodes);
            }
        }
        if (serverNodeChange == ServerNodeChange.ON_INIT) {
            for (ScheduleServerChangeListener listener : this.scheduleServerChangeListeners) {
                listener.onInit();
            }
        }
        if (serverNodeChange == ServerNodeChange.ON_DESTROY) {
            for (ScheduleServerChangeListener listener : this.scheduleServerChangeListeners) {
                listener.onDestroy();
            }
        }
        if (serverNodeChange == ServerNodeChange.ON_OFFLINE) {
            for (ScheduleServerChangeListener listener : this.scheduleServerChangeListeners) {
                listener.onOffline(this.getCurrentOfflineServerNodes());
            }
        }
        if (serverNodeChange == ServerNodeChange.ON_ONLINE) {
            for (ScheduleServerChangeListener listener : this.scheduleServerChangeListeners) {
                listener.onOnline();
            }
        }
    }

    public abstract Set<String> getCurrentServerNodes();

    public abstract Set<String> getCurrentOfflineServerNodes();

    public abstract void init();

    public abstract void destroy();

    public abstract void offlineServer();

    public abstract void onlineServer();

    protected byte[] createServerNodeData() {
        return ByteUtils.toBytes(DateUtils.format(new Date()));
    }
}
